<?php
// No direct access
defined('_JEXEC') or die;

// Load libs
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('formbehavior.chosen', 'select');
JHTML::_('behavior.modal');

// Загрузка параметров формы.
$params = $this->form->getFieldsets('params');
?>
<script type="text/javascript">

    Joomla.submitbutton = function (task) {
        if (task == 'calc.cancel' || document.formvalidator.isValid(document.id('calc-form'))) {
            Joomla.submitform(task, document.getElementById('calc-form'));
        }
    }

    var max_id = 0;

    // Удаление предопределенной переменной
    function del_row(row) {
        if (confirm('<?php echo JText::_('COM_CALC_CALC_PREDEFINED_VAR_DELETE'); ?>')) {
            var line = jQuery('#var_line_' + row);
            line.remove();
        }
    }

    // Добавление предопределенной переменной
    function add_row(row) {
        if (max_id < row)
            max_id = row;
        var new_tr = new Element('tr', {'id': 'var_line_' + max_id});
        var new_td1 = new Element('td', {'width': '100', 'align': 'left', 'class': 'key'});
        var new_td2 = new Element('td', {});
        jQuery('#vari').append(new_tr);
        jQuery(new_tr).append(new_td1);
        jQuery(new_tr).append(new_td2);

        var new_lab = new Element('label', {'data-original-title': '<?php echo JText::_('COM_CALC_CALC_VAR_NAME_DESC'); ?>',
            'for': 'calc_vars[' + max_id + '][1]', 'class': 'control-label hasTooltip required', 'text': '<?php echo JText::_('COM_CALC_CALC_VAR_NAME'); ?>'});
        jQuery(new_td1).append(new_lab);

        var new_name = new Element('input', {'class': 'input-small required', 'type': 'text', 'name': 'calc_vars[' + max_id + '][1]',
            'required': 'required', 'id': 'calc_vars[' + max_id + '][1]', 'size': '32', 'maxlength': '250'});
        jQuery(new_td1).append(new_name);

        var new_del = new Element('button', {'class': 'btn btn-danger', 'type': 'button', text: '<?php echo JText::_('JACTION_DELETE'); ?>',
            'onClick': 'del_row(' + max_id + ')'});
        jQuery(new_td1).append(new_del);

        var new_area = new Element('textarea', {'name': 'calc_vars[' + max_id + '][2]', 'cols': '32', 'rows': '5', 'class': 'text_area span12 hasTooltip',
            'id': 'calc_vars[' + max_id + '][2]', 'data-original-title': '<?php echo JText::_('COM_CALC_CALC_VAR_INPUT_DESC'); ?>'});
        jQuery(new_td2).append(new_area);

        max_id++;
        //alert('new!' + max_id);
    }


</script>
<form action="<?php echo JRoute::_('index.php?option=com_calc&layout=edit&id=' . (int) $this->item->id); ?>" method="post" name="adminForm" id="calc-form" class="form-validate">

    <div class="row-fluid">

        <div class="span12 form-horizontal">

            <ul class="nav nav-tabs">
                <li class="active"><a href="#general" data-toggle="tab"><?php echo JText::_('COM_CALC_CALC_DETAILS'); ?></a></li>
                <?php foreach ($params as $name => $fieldset): ?>
                    <li><a href="#params-<?php echo $name; ?>" data-toggle="tab"><?php echo JText::_($fieldset->label); ?></a></li>
                <?php endforeach; ?>
                <?php if ($this->canDo->get('core.admin')): ?>
                    <li><a href="#permissions" data-toggle="tab"><?php echo JText::_('COM_CALC_FIELDSET_RULES'); ?></a></li>
                <?php endif ?>
            </ul>

            <div class="tab-content">

                <div class="tab-pane active" id="general">
                    <div class="row-fluid form-horizontal-desktop">
                        <div class="span8">
                            <fieldset>
                                <legend><?php echo JText::_('COM_CALC_CALC_DETAILS'); ?></legend>

                                <?php foreach ($this->form->getFieldset('details') as $field): ?>
                                    <?php echo $field->getControlGroup(); ?>
                                <?php endforeach; ?>

                            </fieldset>
                        </div>
                        <div class="span4">
                            <fieldset>
                                <legend><?php echo JText::_('COM_CALC_CALC_PREDEFINED_VARS'); ?></legend>
                                <table class="admintable table table-striped" id="vari" style="border-left:1px solid #6d6d6d;">
                                    <?php
                                    $max_id = 1;
                                    foreach ($this->calc_vars as $calc_var) {
                                        if ($calc_var->id > $max_id)
                                            $max_id = $calc_var->id;
                                        ?>
                                        <tr id="var_line_<?php echo $calc_var->id; ?>">
                                            <td width="100" align="left" class="key">
                                                <label data-original-title="<?php echo JText::_('COM_CALC_CALC_VAR_NAME_DESC'); ?>" 
                                                       for="calc_vars[<?php echo $calc_var->id; ?>][1]" class="control-label hasTooltip required" title="">
                                                           <?php echo JText::_('COM_CALC_CALC_VAR_NAME'); ?>
                                                </label>
                                                <input class="input-small required" required="required" type="text" 
                                                       name="calc_vars[<?php echo $calc_var->id; ?>][1]" id="calc_vars[<?php echo $calc_var->id; ?>][1]" 
                                                       size="32" maxlength="250" value="<?php echo $calc_var->name; ?>" />
                                                <button class="btn btn-danger" style="margin-top:10px;" type="button" onclick="del_row('<?php echo $calc_var->id; ?>')">
                                                    <?php echo JText::_('JACTION_DELETE'); ?>
                                                </button>
                                            </td>
                                            <td>
                                                <textarea name="calc_vars[<?php echo $calc_var->id; ?>][2]" cols="80" rows="5" class="text_area span12 hasTooltip"
                                                          data-original-title="<?php echo JText::_('COM_CALC_CALC_VAR_INPUT_DESC'); ?>"
                                                          id="calc_vars[<?php echo $calc_var->id; ?>][2]"><?php echo $calc_var->vvalues; ?></textarea>
                                            </td>
                                        </tr>
                                    <?php }; ?>
                                </table>
                                <button class="btn" type="button" onclick="add_row('<?php echo $max_id + 1; ?>')"><?php echo JText::_('JACTION_CREATE'); ?></button>
                            </fieldset>
                        </div>
                    </div>

                </div>

                <?php foreach ($params as $name => $fieldset): ?>
                    <div class="tab-pane" id="params-<?php echo $name; ?>">
                        <?php if (isset($fieldset->description) && trim($fieldset->description)): ?>
                            <p class="tip"><?php echo $this->escape(JText::_($fieldset->description)); ?></p>
                            <?php
                        endif;
                        foreach ($this->form->getFieldset($name) as $field) :
                            ?>
                            <div class="control-group">
                                <?php echo $field->label; ?>
                                <div class="controls">
                                    <?php echo $field->input; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endforeach; ?>

                <?php if ($this->canDo->get('core.admin')): ?>
                    <div class="tab-pane" id="permissions">
                        <fieldset>
                            <?php echo $this->form->getInput('rules'); ?>
                        </fieldset>
                    </div>
                <?php endif; ?>

            </div>

        </div>

        <div>
            <input type="hidden" name="task" value="calc.edit" />
            <?php echo JHtml::_('form.token'); ?>
        </div>

    </div>
</form>
